package sino.flux.controller;

import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.r2dbc.core.DatabaseClient;
import org.springframework.data.relational.core.query.Criteria;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import reactor.core.publisher.Flux;
import sino.flux.vo.Product;
import sino.flux.vo.Todo;


@RestController
@RequestMapping("/dbc")
public class DatabaseClientController {
	private static final Logger log = LogManager.getLogger();
	
	
	/**
	 * https://docs.spring.io/spring-data/r2dbc/docs/1.0.x/reference/html/#r2dbc.datbaseclient
	 */
	@Autowired
	private DatabaseClient databaseClient;
	
	
	
	/**
	 * 条件查询
	 * 
	 * db.execute("SELECT id, name, state FROM table WHERE age IN (:ages)")
    			.bind("ages", Arrays.asList(35, 50));
	 * 
	 * @return
	 */
	@GetMapping("/1")
	private Flux<Todo> get1() {
		
		Flux<Todo> flux = this.databaseClient.select()
				.from(Todo.class)
				.matching(Criteria.where("description").is("ceshi")    
					    .and("done").in("true"))
				.fetch()
				.all();
		
		return flux;
	}
	
	
	
	
	
	
	// 普通的SpringMVC方法
	@GetMapping("/2")
	private Flux<Product> get2() {
		
		Flux<Product> flux = this.databaseClient.execute("SELECT * FROM t_product")
				.as(Product.class)
				.fetch()
				.all();	
		return flux;
	}
	
	
	
	// 普通的SpringMVC方法
	@GetMapping("/3")
	private Flux<Map<String,Object>> get3() {
		
		Flux<Map<String,Object>> flux = this.databaseClient.select()
				.from("t_product")
			.fetch()
			.all();	
		return flux;
	}
	
}
